package com.wentao.thisService.service.impl;

import com.wentao.thisFacade.domain.result.LocationCityResult;
import com.wentao.thisFacade.domain.result.LocationDistrictResult;
import com.wentao.thisFacade.domain.result.LocationProvinceResult;
import com.wentao.thisService.mapper.LocationDao;
import com.wentao.thisService.model.LocationEntity;
import com.wentao.thisService.service.CacheLocationService;
import com.wentao.thisService.service.LocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: ZhaiYanTao
 * @Date: 2020/3/27 10:20
 * @Description: 地址定位Service实现
 */
@Service
public class LocationServiceImpl implements LocationService {

    @Autowired
    private LocationDao locationDao;

    @Autowired
    private CacheLocationService cacheLocationService;

    /**
     * @see LocationService#listThreeLevelLocation()
     */
    @Override
    public List<LocationProvinceResult> listThreeLevelLocation() {
        List<LocationEntity> locationEntityList = locationDao.SearchAll();

        LocationProvinceResult locationProvinceResult;
        LocationCityResult locationCityResult;
        LocationDistrictResult locationDistrictResult;

        Map<String, LocationProvinceResult> provinceCodeResultMap = new HashMap<>();

        if (null != locationEntityList && locationEntityList.size() > 0) {
            for (LocationEntity locationEntity : locationEntityList) {
                if (null != locationEntity) {
                    if (!provinceCodeResultMap.containsKey(locationEntity.getProvinceCode())) {
                        locationDistrictResult = new LocationDistrictResult();
                        locationDistrictResult.setDistrictCode(locationEntity.getDistrictCode());
                        locationDistrictResult.setDistrictName(locationEntity.getDistrictName());

                        locationCityResult = new LocationCityResult();
                        locationCityResult.setCityCode(locationEntity.getCityCode());
                        locationCityResult.setCityName(locationEntity.getCityName());
                        locationCityResult.getDistrictList().add(locationDistrictResult);

                        locationProvinceResult = new LocationProvinceResult();
                        locationProvinceResult.setProvinceCode(locationEntity.getProvinceCode());
                        locationProvinceResult.setProvinceName(locationEntity.getProvinceName());
                        locationProvinceResult.getCityCodeResultMap().put(locationEntity.getCityCode(), locationCityResult);

                        provinceCodeResultMap.put(locationEntity.getProvinceCode(), locationProvinceResult);
                    } else if (provinceCodeResultMap.containsKey(locationEntity.getProvinceCode())
                            && !provinceCodeResultMap.get(locationEntity.getProvinceCode()).getCityCodeResultMap().containsKey(locationEntity.getCityCode())) {
                        locationDistrictResult = new LocationDistrictResult();
                        locationDistrictResult.setDistrictCode(locationEntity.getDistrictCode());
                        locationDistrictResult.setDistrictName(locationEntity.getDistrictName());

                        locationCityResult = new LocationCityResult();
                        locationCityResult.setCityCode(locationEntity.getCityCode());
                        locationCityResult.setCityName(locationEntity.getCityName());
                        locationCityResult.getDistrictList().add(locationDistrictResult);

                        provinceCodeResultMap.get(locationEntity.getProvinceCode()).getCityCodeResultMap().put(locationEntity.getCityCode(), locationCityResult);
                    } else {
                        locationDistrictResult = new LocationDistrictResult();
                        locationDistrictResult.setDistrictCode(locationEntity.getDistrictCode());
                        locationDistrictResult.setDistrictName(locationEntity.getDistrictName());

                        provinceCodeResultMap.get(locationEntity.getProvinceCode()).getCityCodeResultMap().get(locationEntity.getCityCode()).getDistrictList().add(locationDistrictResult);
                    }
                }
            }
        }

        List<LocationProvinceResult> provinceList = new ArrayList<>(provinceCodeResultMap.values());
        if (provinceList.size() > 0) {
            for (LocationProvinceResult provinceResult : provinceList) {
                if (null != provinceResult) {
                    provinceResult.setCityList(new ArrayList<>(provinceResult.getCityCodeResultMap().values()));
                    provinceResult.setCityCodeResultMap(null);
                }
            }
        }

        return provinceList;
    }

    /**
     * @see LocationService#listThreeLevelLocation()
     */
    @Override
    public boolean putThreeLevelLocationToCache() {
        List<LocationProvinceResult> provinceResultList = listThreeLevelLocation();
        for (LocationProvinceResult locationProvinceResult : provinceResultList) {
            if (null != locationProvinceResult) {
                cacheLocationService.putToLocationProvinceCache(locationProvinceResult);
            }
        }
        return true;
    }
}
